package com.woniuxy.rms.config;

import com.woniuxy.rms.realm.CustomRealm;
import com.woniuxy.rms.util.HashIteration;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;
/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/23 0023:下午 8:52  <br/>
 * Description:shiro配置类
 */
@Configuration
public class ShiroConfig {
    //配置安全管理器
    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm customRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customRealm);//注入自定义域
        return  securityManager;
    }

    //配置自定义域
    @Bean
    public CustomRealm customRealm(HashedCredentialsMatcher matcher, MemoryConstrainedCacheManager cacheManager){
        CustomRealm customRealm = new CustomRealm();
        customRealm.setCredentialsMatcher(matcher);//注入密码管理器
        customRealm.setCacheManager(cacheManager);//注入缓存管理
        return customRealm;
    }

    //配置过滤器
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean filterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        filterFactoryBean.setSecurityManager(securityManager);//注入安全管理器
        filterFactoryBean.setLoginUrl("/login");
        Map map = new LinkedHashMap();
        map.put("/loginOut","anon");
        map.put("/node","anon");
        map.put("/login/register","anon");
        map.put("/register","anon");
        map.put("/login/checkCode","anon");
        map.put("/system/logout","logout");
        map.put("/src/**","anon");
        map.put("/swagger-ui.html","roles[超级管理员]");
        map.put("/v2/api-docs","roles[超级管理员]");
        map.put("/toWord","roles[超级管理员]");
        map.put("/**","authc");
        filterFactoryBean.setFilterChainDefinitionMap(map);
        return filterFactoryBean;
    }

    //配置密码管理器
    @Bean
    public HashedCredentialsMatcher matcher (){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("MD5");
        matcher.setHashIterations(HashIteration.NUM);
        return matcher;
    }
    //配置缓存器
    @Bean
    public MemoryConstrainedCacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }
    //记住我配置
    @Bean
    public SimpleCookie simpleCookie(){
        SimpleCookie simpleCookie = new SimpleCookie();
        simpleCookie.setMaxAge(1800);
        simpleCookie.setName("rememberMe");
        return simpleCookie;
    }
    @Bean
    public CookieRememberMeManager rememberMeManager(SimpleCookie cookie){
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(cookie);
        return cookieRememberMeManager;
    }

    //解决shiro 整合 spring aop 的时候，访问资源404的问题
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setUsePrefix(true);
        return defaultAdvisorAutoProxyCreator;
    }
}